package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Application;
import cn.hrlly.tao2shou.listener.OnApplicationDetailsListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-31
 * Time: 11:41
 */

public class ApplicationDetailsCallback extends Callback<Application> {

    private OnApplicationDetailsListener onApplicationDetailsListener;

    public ApplicationDetailsCallback(OnApplicationDetailsListener onApplicationDetailsListener){
        this.onApplicationDetailsListener=onApplicationDetailsListener;
    }

    @Override
    public Application parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        Application application=new Gson().fromJson(string,Application.class);
        return application;
    }

    @Override
    public void onError(Request request, Exception e) {
        onApplicationDetailsListener.showApplicationDetailsFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(Application application) {
        if(application.getStatus()==0)
            onApplicationDetailsListener.showApplicationDetailsSucc(application);
        else
            onApplicationDetailsListener.showApplicationDetailsFail(application.getErrorMessage());
    }
}
